package com.hongyi.threadPoolExcutor;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: hongyi
 * @CreateTime: 2024-10-23
 */


public class ThreadPoolExecutorExample {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2,
                4,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>()
        );

        // 条件任务
        for (int i = 0; i < 10; i++) {
            final int finalI = i;
            threadPoolExecutor.submit(() -> {
                        System.out.println("正在执行任务" + finalI);
                        try {
                            Thread.sleep(1);

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            // 恢复中断状态 (中断状态会被清除)
                            Thread.currentThread().interrupt();
                        }
                    }
            );
        }

        // 关闭线程池
        threadPoolExecutor.shutdown();
    }
}
